package lib.Com;

public class Network extends Thread {
	/**
	 * Class to start/stop procedure of the different network objects
	 */
	private Container container;
	private BroadcastListener broadcastListener = new BroadcastListener();
	private Server server = new Server();
	private BroadcastSelfSchedule broadcaster = new BroadcastSelfSchedule();

	private boolean running = true;

	public Network(String UserId) {
		super("Network");
		container = new Container(UserId);
		try {
			container.start();
			Thread.sleep(50);
			broadcastListener.start();
			Thread.sleep(50);
			server.start();
			Thread.sleep(50);
			broadcaster.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void run() {
		do {
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
			}
		} while (running);
	}

	public void Stop() {
		try {
			container.Stop();
			Thread.sleep(50);
			broadcastListener.Stop();
			Thread.sleep(50);
			server.Stop();
			Thread.sleep(50);
			broadcaster.Stop();
			Thread.sleep(50);
		} catch (Exception e) {
			e.printStackTrace();
		}
		running = false;
	}
}
